package com.example.zx.theline.jclr.pub;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


import com.example.zx.theline.R;
import com.example.zx.theline.jclr.JclrActivity;
import com.example.zx.theline.lyjh.lyzjh.pub.SgzjhShenheDetailActivity;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import GsonBean.JcbAqfxListBean;
import GsonBean.JcbShdwListBean;
import GsonBean.JcxxChakanBean;
import GsonBean.RjhSgxmListBean;
import GsonBean.SuccessBean;
import GsonBean.post.QdzgrPost;
import Utils.MyToast;
import Utils.RefreshManager;
import app.MyApplication;
import automatchingview.AutoMatchingPopuWindow;
import base.BaseActivity;
import dialog.FgshDialog;
import dialog.FgysDialog;
import dialog.IsShutDownAuditDialog;
import dialog.TgysDialog;
import dialog.TjjdjcdDialog;
import dialog.TxzgqkDialog;
import dialog.ZdzgrDialog;
import dialog.ZgshDialog;
import greendao.UserDao;
import greendao.entity.User;
import network.BaseCallBack;
import network.NetWorkApi;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import views.AutoMatchingPopuClickListener;
import views.AutoMatchingPopuWindowSingle;
import views.LzxxItemView;

public class JcxxEditActivity extends BaseActivity implements View.OnClickListener{

    //控制检查详情 layout visiavle
    RadioButton jcxx_button;

    //流转信息详情 layout visiavle
    RadioButton lzxx_button;


    ScrollView lzxx_layout;
    ScrollView jcxx_layout;

    //日计划编号
    String rjdjhbBh;

    //检查表ID
    String jdjhjcbId;

    //月度编号
    String ydbh;
    TextView ydbhtv;

    //信息类型
    TextView xxlx;

    //日计划号
    TextView rjhh;

    //类别
    TextView lb;

    //责任单位
    TextView zrdw;

    //施工单位
    TextView sgdw;

    //施工地点
    TextView sgdd;

    //施工项目
    TextView sgxm;

    //检查日期
    TextView jcrq;

    //检查人
    TextView jcr;

    //计划监护人
    TextView jhjhr;

    //录入人
    TextView lrr;

    //检查地点
    TextView jcdd;

    //风险列表
    LinearLayout fxlb;

    //整体评价
    TextView ztpj;

    //检查情况
    TextView jcqk;


    //发现时间
    TextView fxsj;

    //通知时间
    TextView tzsj;

    //发令单位
    TextView fldw;

    //发令人
    TextView flr;

    //停工项目
    TextView tgxm;


    //发现问题
    TextView fxwt;

    TextView zgqk;

    LinearLayout wlxx;

    //取消录入
    TextView qxlr;

    //底部四个权限按钮
    //【填写整改情况】
    TextView txzgqk;

    //【提交】
    TextView tj;

    //【指定整改人】
    TextView zdzgr;

    //【停工审核】
    TextView tgsh;

    //【停工预审】
    TextView tgys;

    //【复工预审】
    TextView fgys;

    //【复工审核】
    TextView fgsh;

    //【整改审核】
    TextView zgsh;

    TextView leftTextView;

    TextView centerTextView;



    JcxxChakanBean mBean;

    //整改情况 layout
    LinearLayout zgqk_layout;

    //停工项目 layout
    LinearLayout tgxm_layout;

    //复工项目 layout
    LinearLayout fgxm_layout;


    //复工项目
    TextView fgxm;
    List<RjhSgxmListBean> sgxmList;

    private  TjjdjcdDialog mTjjdjcdDialog;
    private TxzgqkDialog mTxzgqkDialog;
    private TgysDialog mTgysDialog;
    private  FgysDialog mFgysDialog;
    private FgshDialog mFgshDialog;
    private ZgshDialog mZgshDialog;

    private PopupWindow mPopu;
    private AutoMatchingPopuWindowSingle window;
    private List<User> mSourceList;
    private UserDao mUserDao;
    //确定整改人PostBean
    QdzgrPost mQdzgrPost;
    //选中的整改人
    private List<User> mSelectUserList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jcxx_edit);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {
        xxlx = (TextView) findViewById(R.id.xxlx);
        rjhh = (TextView) findViewById(R.id.rjhh);
        ydbhtv = (TextView) findViewById(R.id.ydbh);
        lb = (TextView) findViewById(R.id.lb);
        zrdw = (TextView) findViewById(R.id.zrdw);
        sgdw = (TextView) findViewById(R.id.sgdw);
        sgdd = (TextView) findViewById(R.id.sgdd);
        sgxm = (TextView) findViewById(R.id.sgxm);
        sgdd = (TextView) findViewById(R.id.sgdd);
        jcrq = (TextView) findViewById(R.id.jcrq);
        jcr = (TextView) findViewById(R.id.jcr);
        lrr = (TextView) findViewById(R.id.lrr);
        jcdd = (TextView) findViewById(R.id.jcdd);
        fxlb = (LinearLayout) findViewById(R.id.fxlb);
        ztpj = (TextView) findViewById(R.id.ztpj);
        jcqk = (TextView) findViewById(R.id.jcqk);
        jhjhr = (TextView) findViewById(R.id.jhjhr);
        wlxx = (LinearLayout) findViewById(R.id.lzxx);
        fxsj = (TextView) findViewById(R.id.fxsj);
        tzsj = (TextView) findViewById(R.id.tzsj);
        fldw = (TextView) findViewById(R.id.fldw);
        flr = (TextView) findViewById(R.id.flr);
        tgxm = (TextView) findViewById(R.id.tgxm);
        fgxm = (TextView) findViewById(R.id.fgxm);
        fxwt = (TextView) findViewById(R.id.fxwt);
        leftTextView = (TextView) findViewById(R.id.leftTextView);
        centerTextView = (TextView) findViewById(R.id.centerTextView);

        qxlr = (TextView) findViewById(R.id.qxlr);
        qxlr.setOnClickListener(this);

        zgqk_layout = (LinearLayout) findViewById(R.id.zgqk_layout);
        tgxm_layout = (LinearLayout) findViewById(R.id.tgxm_layout);
        fgxm_layout = (LinearLayout) findViewById(R.id.fgxm_layout);

        zgqk = (TextView) findViewById(R.id.zgqk);

        jcxx_button = (RadioButton) findViewById(R.id.jcxx_button);
        lzxx_button = (RadioButton) findViewById(R.id.lzxx_button);

        lzxx_layout = (ScrollView) findViewById(R.id.lzxx_layout);
        jcxx_layout = (ScrollView) findViewById(R.id.jcxx_layout);



        //5个按钮
        txzgqk = (TextView) findViewById(R.id.txzgqk);
        txzgqk.setOnClickListener(this);
        tj = (TextView) findViewById(R.id.tj);
        tj.setOnClickListener(this);
        zdzgr = (TextView) findViewById(R.id.zdzgr);
        zdzgr.setOnClickListener(this);
        tgsh = (TextView) findViewById(R.id.tgsh);
        tgsh.setOnClickListener(this);
        tgys = (TextView) findViewById(R.id.tgys);
        tgys.setOnClickListener(this);
        fgys = (TextView) findViewById(R.id.fgys);
        fgys.setOnClickListener(this);
        fgsh = (TextView) findViewById(R.id.fgsh);
        fgsh.setOnClickListener(this);
        zgsh = (TextView) findViewById(R.id.zgsh);
        zgsh.setOnClickListener(this);
        leftTextView.setOnClickListener(this);
        centerTextView.setOnClickListener(this);

    }

    @Override
    protected void InitData() {
        mUserDao = MyApplication.getDaoSession().getUserDao();
        mSourceList = mUserDao.queryBuilder()
                .where(UserDao.Properties.LjyyxDwxxId.eq(MyApplication.getmLoginUser().getUserUnitId()))
                .list();

        Bundle mBundle=getIntent().getExtras();
        rjdjhbBh=mBundle.getString("rjdjhbBh");
        jdjhjcbId=mBundle.getString("jdjhjcbId");
        sgxmList=new ArrayList<>();
        getInfo();

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.leftTextView:
                //取消录入
                finish();
                break;
            case R.id.qxlr:
                //取消录入
                finish();
                break;
            case R.id.tgsh:
                //停工审核  审核弹出框中增加进行选择停工/复工的施工项目
                stopAudit();
                break;
            case R.id.zdzgr:
                //指定整改人
                showAddPopu();
                break;
            case R.id.tj:
                //提交
                mTjjdjcdDialog = new TjjdjcdDialog( JcxxEditActivity.this, mBean,JcxxEditActivity.this);
                mTjjdjcdDialog.show();
                break;
            case R.id.txzgqk:
                //填写整改情况
                mTxzgqkDialog = new TxzgqkDialog( JcxxEditActivity.this, mBean,JcxxEditActivity.this);
                mTxzgqkDialog.show();
                break;
            case R.id.tgys:
                //停工预审
                mTgysDialog = new TgysDialog(JcxxEditActivity.this, mBean,JcxxEditActivity.this);
                mTgysDialog.show();
                break;
            case R.id.fgys:
                //复工预审
                mFgysDialog = new FgysDialog(JcxxEditActivity.this, mBean,JcxxEditActivity.this);
                mFgysDialog.show();
                break;
            case R.id.fgsh:
                //复工审核 审核弹出框中增加进行选择停工/复工的施工项目
                mFgshDialog = new FgshDialog(JcxxEditActivity.this, mBean,JcxxEditActivity.this);
                mFgshDialog.show();
                break;
            case R.id.zgsh:
                //整改审核
                mZgshDialog = new ZgshDialog(JcxxEditActivity.this, mBean,JcxxEditActivity.this);
                mZgshDialog.show();
                break;
            default:
                break;
        }
    }

    public void stopAudit(){
        String tgxmid = "";
        for (int i = 0; i < mBean.getRjhSgxmList().size(); i++) {
            if(mBean.getRjhSgxmList().get(i).isTgXm())
                tgxmid += mBean.getRjhSgxmList().get(i).getSgxmlbzdId()+",";
        }
        if(tgxmid.length()>0)
            tgxmid = tgxmid.substring(0, tgxmid.length() - 1);
        IsShutDownAuditDialog mIsShutDownAuditDialog = new IsShutDownAuditDialog( JcxxEditActivity.this, tgxm.getText().toString(), tgxmid, mBean);
        mIsShutDownAuditDialog.show();
    }
    @Override
    protected void InitListen() {
        jcxx_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    jcxx_layout.setVisibility(View.VISIBLE);
                    lzxx_layout.setVisibility(View.GONE);
                }
            }
        });
        lzxx_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    lzxx_layout.setVisibility(View.VISIBLE);
                    jcxx_layout.setVisibility(View.GONE);
                }
            }
        });

    }
    public void getInfo() {
        //日计划号：rjdjhbBh
        //监督计划表主键：jdjhjcbId
        //登录用户名：loginId
        //传三个参数
        MyApplication.ShowWaitDialog(this);
        Map<String,String> map=new HashMap<>();
        map.put("rjdjhbBh", rjdjhbBh);
        map.put("loginId", MyApplication.getmLoginUser().getUserDisplayName());
        map.put("jdjhjcbId", jdjhjcbId);
        NetWorkApi.getJcxxChakanList(map, new BaseCallBack<JcxxChakanBean>(){

                    @Override
                    public void Success(Call<JcxxChakanBean> call, Response<JcxxChakanBean> response) {
                        try {
                            MyApplication.HideWaitDialog();
                            JcxxChakanBean mJcxxChakanBean = response.body();
                            mBean = mJcxxChakanBean;

                            //只有停工项目 才显示该内容
                            if (mBean.getRjdjhjcbInfo().getTgxmmc() == null) {
                                tgxm.setVisibility(View.GONE);
                            } else {
                                tgxm.setVisibility(View.VISIBLE);
                                tgxm.setText(mJcxxChakanBean.getRjdjhjcbInfo().getTgxmmc());
                            }


                            if (mBean.getRjdjhjcbInfo().getFgXmMc() == null || mBean.getRjdjhjcbInfo().getFgXmMc().equals("")) {
                                fgxm_layout.setVisibility(View.GONE);
                            } else {
                                fgxm.setVisibility(View.VISIBLE);
                                fgxm.setText(mJcxxChakanBean.getRjdjhjcbInfo().getFgXmMc());
                            }

                            if (mJcxxChakanBean.getRjdjhjcbInfo().isJcbTgAuditBtnHide() || mJcxxChakanBean.getRjdjhjcbInfo().isJcbFgAuditBtnHide()) {
                                tgxm_layout.setVisibility(View.VISIBLE);
                            }
                            ydbh = mJcxxChakanBean.getRjdjhbInfo().getYdbh();
                            ydbhtv.setText(ydbh);
                            xxlx.setText(mJcxxChakanBean.getRjdjhjcbInfo().getJcbtype());
                            rjhh.setText(mJcxxChakanBean.getRjdjhbInfo().getBh());
                            lb.setText(mJcxxChakanBean.getRjdjhbInfo().getLb());
                            zrdw.setText(mJcxxChakanBean.getRjdjhjcbInfo().getZrDwMc());
                            sgdw.setText(mJcxxChakanBean.getRjdjhbInfo().getSgdwmc());
                            sgdd.setText(mJcxxChakanBean.getRjdjhbInfo().getSgdd());
                            sgxm.setText(mJcxxChakanBean.getRjdjhbInfo().getXmmc());
                            sgdd.setText(mJcxxChakanBean.getRjdjhbInfo().getSgdd());
                            jcrq.setText(mJcxxChakanBean.getRjdjhjcbInfo().getJcrq());
                            jcr.setText(mJcxxChakanBean.getRjdjhjcbInfo().getJcrName());
                            lrr.setText(mJcxxChakanBean.getRjdjhjcbInfo().getLrrName());
                            jcdd.setText(mJcxxChakanBean.getRjdjhjcbInfo().getJcdd());
                            jhjhr.setText(mJcxxChakanBean.getRjdjhjcbInfo().getJhjhr());
                            fxsj.setText(mJcxxChakanBean.getRjdjhjcbInfo().getFxsj());
                            tzsj.setText(mJcxxChakanBean.getRjdjhjcbInfo().getTzsj());
                            fldw.setText(mJcxxChakanBean.getRjdjhjcbInfo().getFldw());
                            flr.setText(mJcxxChakanBean.getRjdjhjcbInfo().getFlrName());

                            //只有有整改情况  才显示该内容
                            if (mJcxxChakanBean.getRjdjhjcbInfo().getZgqk() != null) {
                                zgqk_layout.setVisibility(View.VISIBLE);
                                zgqk.setText(mJcxxChakanBean.getRjdjhjcbInfo().getZgqk());
                            }

                            fxwt.setText(mJcxxChakanBean.getRjdjhjcbInfo().getFxwt());


                            //安全风险list 填充
                            for (JcbAqfxListBean mBean : mJcxxChakanBean.getJcbAqfxList()) {
                                View view = View.inflate(JcxxEditActivity.this, R.layout.fxlb_chakan, null);
                                TextView aqfxnr = (TextView) view.findViewById(R.id.aqfxnr);
                                TextView fxpj = (TextView) view.findViewById(R.id.fxpj);
                                aqfxnr.setText(mBean.getAqfxnr());
                                fxpj.setText(getAqfxnr(Integer.parseInt(mBean.getAqfxpj())));
                                fxlb.addView(view);
                            }
                            ztpj.setText(mJcxxChakanBean.getRjdjhjcbInfo().getZtpj());
                            jcqk.setText(mJcxxChakanBean.getRjdjhjcbInfo().getJcqk());

                            //流转信息list 填充
                            if (mJcxxChakanBean.getJcbShdwList() != null) {
                                for (int j = 0; j < mJcxxChakanBean.getJcbShdwList().size(); j++) {
                                    JcbShdwListBean mBean = mJcxxChakanBean.getJcbShdwList().get(j);
                                    LzxxItemView mView = new LzxxItemView(JcxxEditActivity.this, mBean.getShrxx(), j);
                                    wlxx.addView(mView);
                                }
                            }
                            /**
                             * 根据传来的权限，控制按钮
                             * */

                            //停工预审
                            if (mJcxxChakanBean.getRjdjhjcbInfo().isJcbTgPreAuditBtnHide())
                                tgys.setVisibility(View.VISIBLE);
                            //停工审核
                            if (mJcxxChakanBean.getRjdjhjcbInfo().isJcbTgAuditBtnHide())
                                tgsh.setVisibility(View.VISIBLE);
                            //指定整改人
                            if (mJcxxChakanBean.getRjdjhjcbInfo().isJcbZdZgrBtnHide())
                                zdzgr.setVisibility(View.VISIBLE);
                            //填写整改情况
                            if (mJcxxChakanBean.getRjdjhjcbInfo().isJcbZgQkBtnHide())
                                txzgqk.setVisibility(View.VISIBLE);
                            //提交
                            if (mJcxxChakanBean.getRjdjhjcbInfo().isJcbZgTjBtnHide())
                                tj.setVisibility(View.VISIBLE);
                            //复工预审
                            if (mJcxxChakanBean.getRjdjhjcbInfo().isJcbFgPreAuditBtnHide())
                                fgys.setVisibility(View.VISIBLE);
                            //复工审核
                            if (mJcxxChakanBean.getRjdjhjcbInfo().isJcbFgAuditBtnHide())
                                fgsh.setVisibility(View.VISIBLE);
                            //整改审核
                            if (mJcxxChakanBean.getRjdjhjcbInfo().isJcbZgAuditBtnHide())
                                zgsh.setVisibility(View.VISIBLE);
                        } catch (Exception e) {
                            MyToast.showFail();

                        }
                    }

                    @Override
                    public void Failure(Call<JcxxChakanBean> call, Throwable t) {
                        MyToast.showFail();
                    }

                    @Override
                    public void onFinally() {
                        MyApplication.HideWaitDialog();
                    }
                }

        );
    }

    //指定整改人 确定按钮
    public void confirmZgr(){
        if (mSelectUserList.size()==0) {
            MyToast.showToast("请选择整改人");
        } else {
            mQdzgrPost=new QdzgrPost();
            mQdzgrPost.setJdjhjcbId(jdjhjcbId);
            mQdzgrPost.setZgrId(mSelectUserList.get(0).getLjyyxLoginName());
            mQdzgrPost.setZgrName(mSelectUserList.get(0).getLjyyxWorkerName());
            mQdzgrPost.setZrDwMc(mSelectUserList.get(0).getLjyyxDwxxName());
            mQdzgrPost.setXgrId(MyApplication.getmLoginUser().getUserDisplayName());

            //审核单位流转信息list
            List<QdzgrPost.LjyyxRjdjhjcbShdw> jcbShdwList = new ArrayList<QdzgrPost.LjyyxRjdjhjcbShdw>();
            QdzgrPost.LjyyxRjdjhjcbShdw shdw = new QdzgrPost.LjyyxRjdjhjcbShdw();
            shdw.setJdjhbBh(rjdjhbBh);
            shdw.setDwId(MyApplication.getmLoginUser().getUserUnitId());
            shdw.setDwmc(MyApplication.getmLoginUser().getUserUnitName());
            shdw.setShrlogin(MyApplication.getmLoginUser().getUserDisplayName());
            shdw.setShrname(MyApplication.getmLoginUser().getTrueName());
            shdw.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());
            jcbShdwList.add(shdw);
            mQdzgrPost.setJcbShdwList(jcbShdwList);
            MyApplication.ShowWaitDialog(this);
            NetWorkApi.Qdzgr(mQdzgrPost, new Callback<SuccessBean>() {
                        @Override
                        public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                            try {
                                SuccessBean mBean = response.body();
                                if (mBean.getMsg().equals("success") && mBean.getCount() != 0) {
                                    MyToast.showToast("指定整改人成功");
                                    RefreshManager.Jdjc.RefreshJdjclrxx();
                                    RefreshManager.Jdjc.RefreshJdjcdcl();
                                    RefreshManager.Jdjc.RefreshJdjcycl();
                                    JcxxEditActivity.this.finish();
                                    JcxxEditActivity.this.startActivity(new Intent(JcxxEditActivity.this, JclrActivity.class));
                                }
                            } catch (Exception e) {
                                MyToast.showFail();
                            }
                            MyApplication.HideWaitDialog();
                        }

                        @Override
                        public void onFailure(Call<SuccessBean> call, Throwable t) {
                            MyToast.showFail();
                        }
                    }
            );
        }
    }

    private void showAddPopu() {

        if (mPopu == null) {
            window = new AutoMatchingPopuWindowSingle();
            window.Builder(this)
                    .setOutsideTouchable(false)
                    .setSourceList(mSourceList)
                    .setEditTextWatcher(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            if (s.length() == 0) {
                                window.UpListViewAdapter(mSourceList);
                            } else {
                                window.UpListViewAdapter(mUserDao.queryBuilder()
                                        .where(UserDao.Properties.LjyyxWorkerName.like("%" + s.toString() + "%"))
                                        .where(UserDao.Properties.LjyyxDwxxId.eq(MyApplication.getmLoginUser().getUserUnitId()))
                                        .list());
                            }
                        }
                    })
                    .setAutoMatchingPopuClickListener(new AutoMatchingPopuClickListener<User>() {
                        @Override
                        public void OnAutoMatchingPopClicked(List<User> list) {
                            if (list.size() == 0) {
                                Toast.makeText(JcxxEditActivity.this, "请选择一个整改人", Toast.LENGTH_SHORT).show();
                            } else {
                                mSelectUserList=list;
                                confirmZgr();
                            }
                            mPopu.dismiss();
                        }
                    });
            mPopu = window.CreatePopu();
        }
        mPopu.showAtLocation(getWindow().getDecorView(), Gravity.RIGHT, 0, 0);
    }


    private String getAqfxnr(int i){

        if(i==1)
        return "好";

        if(i==2)
            return "正常";

        if(i==3)
            return "基本正常";

        if(i==4)
            return "有问题";

        return "好";
    }

}
